﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using SimpleGame.GameObjects;
using SimpleGame.SpriteClasses;
using Microsoft.Xna.Framework.Graphics;
using SimpleGame.Font;
using SimpleGame.Utils;
using Microsoft.Xna.Framework.Input;
using SimpleGameData.GameObjects;
using SimpleGame.GameObjects.Particles;
using SimpleGameData.GameObjects.Particles;
using System.Diagnostics;

namespace SimpleGame.ScreenManagement.Screens
{
    public class GameplayScreen : GameScreen
    {
        #region Fields

        // The content manager which loads all the content for the game
        private ContentManager content;
        // Represents the player 
        private static Player player;

        private Emitter testEmitter;

        #endregion

        #region Constructor

        public GameplayScreen()
        {
        }

        #endregion

        #region Load Methods

        public override void LoadContent()
        {
            if (content == null)
            {
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            }

            if (content != null)
            {
                //new SerializationTest(content);
                CreatePlayer();

                CreateTestEmitter();
            }
        }

        public void CreatePlayer()
        {
            // Load the sprite data from xml file
            AnimatedSpriteData playerSpriteData = content.Load<AnimatedSpriteData>(@"xml/playerSpriteData");
            // Create an animated sprite and pass the xml data
            AnimatedSprite playerSprite = new AnimatedSprite(playerSpriteData);

            // Create a new static player instance
            player = new Player(playerSprite);
        }

        public void CreateTestEmitter()
        {
            EmitterData emitterData = new EmitterData();
            emitterData.ParticleCount = 100;
            emitterData.Texture = content.Load<Texture2D>(@"particle/star");

            testEmitter = new Emitter();
            testEmitter.EmitterData = emitterData;
            testEmitter.AddParticles(emitterData.ParticleCount);
        }

        public override void UnloadContent()
        {
            content.Unload();
        }

        #endregion

        #region Update and Draw Methods

        public override void Update(GameTime gameTime)
        {
            if (isActive)
            {
                if (InputHandler.IsKeyPressed(Keys.Escape))
                {
                    screenManager.AddScreen(new PauseMenuScreen(this));
                    isActive = false;
                    isHidden = true;
                }

                // Update the player
                player.Update(gameTime);

                testEmitter.Update(gameTime);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if (!isHidden)
            {
                // Clear the graphics device in order to make a (re)draw possible
                // TODO Set backgroundcolor back to black. Currently it's green for the developement phase
                screenManager.GraphicsDevice.Clear(Color.Green);

                SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
                // Begin drawing
                spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);

                // Draw the player
                player.Draw(spriteBatch);

                testEmitter.Draw(spriteBatch);

                FontManager.Instance.DrawString(
                    FontManager.Instance.GetFont("default"),
                    "This is a SimpleFont.",
                    new Vector2(50, 100),
                    Color.White,
                    1.0f,
                    0.0f,
                    Vector2.Zero);

                FontManager.Instance.DrawStringShadowed(
                    FontManager.Instance.GetFont("default"),
                    "This is a shadowed SimpleFont.",
                    new Vector2(50, 150),
                    Color.White,
                    1.0f,
                    0.0f,
                    Vector2.Zero,
                    Color.Black,
                    new Vector2(4, 4),
                    1.0f);

                // End drawing
                spriteBatch.End();
            }
        }

        #endregion

    }
}
